package com.ssyc.gsk_teacher.activity;

import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.gsk_teacher.R;

/**
 * 教师端--攻略界面
 */
@Route(path = "/teacher/TeacherStrategyActivity")
public class TeacherStrategyActivity extends BaseActivity implements OnClickListener {

    //返回
    private ImageView iv_back;

    private WebView mWebView;

    private TextView tv_st_title;

    private RelativeLayout rlLoadingView;


    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.base_activity_video_web;
    }

    @Override
    protected void init() {
        initView();

        initWebView();

        initData();
    }


    /**
     * 初始化webview控件
     */
    private void initWebView() {
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setPluginState(PluginState.ON);
        settings.setAllowFileAccess(true);
        settings.setLoadWithOverviewMode(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(new WebViewClient());
    }


    /**
     * 初始化控件
     */
    private void initView() {
        rlLoadingView = findViewById(R.id.rl_loading_view);
        iv_back = findViewById(R.id.iv_back);
        mWebView = findViewById(R.id.wb_video);
        tv_st_title = findViewById(R.id.tv_st_title);
        iv_back.setOnClickListener(this);
    }


    /**
     * 初始化数据
     */
    private void initData() {

        //设置标题
        tv_st_title.setText("攻  略");

        mWebView.reload();

        //新地址
        mWebView.loadUrl(HttpAdress.UPDATE_URL + "JPXB/sharePage/pages/JPXB/strategy/strategy.html");

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                return true;
            }
        });


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (null != rlLoadingView && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }
            }
        }, 500);

        showContent();
    }


    @Override
    public void onPause() {
        super.onPause();
        mWebView.onPause();
        mWebView.pauseTimers();
    }

    @Override
    public void onResume() {
        super.onResume();
        mWebView.resumeTimers();
        mWebView.onResume();
    }

    @Override
    protected void onDestroy() {
        mWebView.destroy();
        mWebView = null;
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int id = v.getId();

        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }
}
